package LeetCode刷题.之前的算法;

import java.util.Arrays;

public class Headtailpointer {
    //首位双指针法
    public static void main(String arg[]) {
        int nums[] = {1, 2, 3, 4};
        nums = exchange(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static int[] exchange(int[] nums) {
        //要求：将奇数放在数组所有数字的前面，偶数在后面
        int len = nums.length;
        int i = 0;
        int j = len - 1;
        while (i < j) {
            if ((nums[i] & 1) != 0) {//如果是奇数的话
                i++;
            } else if ((nums[j] & 1) == 0) {//如果是偶数的话
                j--;
            } else {
                swap(nums, i++, j--);
            }
        }
        return nums;
    }

    public static void swap(int nums[], int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
